package src.view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.jgoodies.forms.layout.*;
import org.jdesktop.swingx.*;
/*
 * Created by JFormDesigner on Tue May 25 21:34:53 ART 2010
 */


import src.controller.ManagePostOfficesController;
import src.resource.PublicResource;




/**
 * @author Guillermo Exposito
 */
public class ManagePostOffices extends JPanel {
	
	/**
	 * 
	 */
	private final String PATH=PublicResource.getPath();
	private static final long serialVersionUID = 1L;
	private ManagePostOfficesController controller;
	private static ManagePostOffices instance=null;
	
	public static ManagePostOffices getInstance(ManagePostOfficesController controller){
		if(instance==null)
			instance=new ManagePostOffices(controller);
		return instance;
	}
	private ManagePostOffices(ManagePostOfficesController controller) {
		this.controller=controller;
		initComponents();
		
		this.btnSave.addActionListener(new ActionUpdatePostOffice());
		this.btnDelete.addActionListener(new ActionDeletePostOffice());
		this.btnSearch.addActionListener(new ActionSearchPostOffices());
		this.jListPostOffice.addListSelectionListener(new ActionSelectItem());
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Guillermo Exposito
		panel2 = new JPanel();
		lblTitle = new JLabel();
		panel1 = new JPanel();
		jPanelSearchUser = new JPanel();
		lblSearchPostOffice = new JLabel();
		label1 = new JLabel();
		txtSearch = new JTextField();
		btnSearch = new JButton();
		scrollPane1 = new JScrollPane();
		jListPostOffice = new JList();
		btnDelete = new JButton();
		jPanelFields = new JPanel();
		lblName = new JLabel();
		txtName = new JTextField();
		label4 = new JLabel();
		label3 = new JLabel();
		btnSave = new JButton();
		CellConstraints cc = new CellConstraints();

		//======== this ========

		
		setLayout(new FormLayout(
			"left:default:grow",
			"fill:default, fill:default:grow"));

		//======== panel2 ========
		{
			panel2.setLayout(new FormLayout(
				"left:420dlu",
				"10dlu, fill:default, 5dlu"));

			//---- lblTitle ----
			lblTitle.setText("Manage Post Offices");
			lblTitle.setFont(new Font("Dialog", Font.PLAIN, 18));
			panel2.add(lblTitle, cc.xywh(1, 2, 1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));
		}
		add(panel2, cc.xy(1, 1));

		//======== panel1 ========
		{
			panel1.setLayout(new HorizontalLayout(1));

			//======== jPanelSearchUser ========
			{
				jPanelSearchUser.setLayout(new FormLayout(
					"20dlu, 22dlu, 3dlu, 100dlu, 3dlu, $lcgap, 62dlu, $lcgap, 5dlu",
					"2*(default, 10dlu), fill:default:grow, $lgap, 10dlu, $lgap, default, $lgap, 10dlu"));

				//---- lblSearchPostOffice ----
				lblSearchPostOffice.setText("Search Post Office");
				lblSearchPostOffice.setFont(new Font("Dialog", Font.PLAIN, 14));
				jPanelSearchUser.add(lblSearchPostOffice, cc.xywh(2, 1, 3, 1));

				//---- label1 ----
				label1.setText("Name");
				jPanelSearchUser.add(label1, cc.xywh(2, 3, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
				jPanelSearchUser.add(txtSearch, cc.xy(4, 3));

				//---- btnSearch ----
				btnSearch.setText("Search");
				jPanelSearchUser.add(btnSearch, cc.xywh(7, 3, 3, 1, CellConstraints.LEFT, CellConstraints.DEFAULT));

				//======== scrollPane1 ========
				{

					//---- jListPostOffice ----
					jListPostOffice.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					scrollPane1.setViewportView(jListPostOffice);
				}
				jPanelSearchUser.add(scrollPane1, cc.xywh(2, 5, 6, 1, CellConstraints.FILL, CellConstraints.FILL));

				//---- btnDelete ----
				btnDelete.setText("Delete");
				jPanelSearchUser.add(btnDelete, cc.xywh(7, 9, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
			}
			panel1.add(jPanelSearchUser);

			//======== jPanelFields ========
			{
				jPanelFields.setBorder(null);
				jPanelFields.setLayout(new FormLayout(
					"30dlu, $lcgap, 150dlu, 5dlu, 10dlu",
					"45dlu, $lgap, default, $lgap, 10dlu, $lgap, default, $lgap, 10dlu"));

				//---- lblName ----
				lblName.setText("Name");
				lblName.setHorizontalAlignment(SwingConstants.RIGHT);
				jPanelFields.add(lblName, cc.xy(1, 3));
				jPanelFields.add(txtName, cc.xy(3, 3));

				//---- label4 ----
				label4.setText("*");
				label4.setForeground(Color.red);
				jPanelFields.add(label4, cc.xy(4, 3));

				//---- label3 ----
				label3.setText("* Required fields");
				label3.setForeground(Color.red);
				jPanelFields.add(label3, cc.xy(3, 5));

				//---- btnSave ----
				btnSave.setText("Update");
				jPanelFields.add(btnSave, cc.xywh(3, 7, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
			}
			panel1.add(jPanelFields);
		}
		add(panel1, cc.xy(1, 2));
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
		
		this.JListModel=new DefaultListModel();
		lblTitle.setIcon(new ImageIcon(PATH+"file-roller.png"));
		btnSave.setIcon(new ImageIcon(PATH+"document-save.png"));
		btnDelete.setIcon(new ImageIcon(PATH+"edit-delete.png"));
		btnSearch.setIcon(new ImageIcon(PATH+"system-search.png"));
		
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Guillermo Exposito
	private JPanel panel2;
	private JLabel lblTitle;
	private JPanel panel1;
	private JPanel jPanelSearchUser;
	private JLabel lblSearchPostOffice;
	private JLabel label1;
	private JTextField txtSearch;
	private JButton btnSearch;
	private JScrollPane scrollPane1;
	private JList jListPostOffice;
	private JButton btnDelete;
	private JPanel jPanelFields;
	private JLabel lblName;
	private JTextField txtName;
	private JLabel label4;
	private JLabel label3;
	private JButton btnSave;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
	
	private ListModel JListModel;

	public JTextField getTxtName() {
		return txtName;
	}
	public void setTxtName(JTextField txtName) {
		this.txtName = txtName;
	}
	public JButton getBtnSave() {
		return btnSave;
	}
	public void setBtnSave(JButton btnSave) {
		this.btnSave = btnSave;
	}
	public JTextField getTxtSearch() {
		return txtSearch;
	}
	public void setTxtSearch(JTextField txtSearch) {
		this.txtSearch = txtSearch;
	}
	public JButton getBtnSearch() {
		return btnSearch;
	}
	public void setBtnSearch(JButton btnSearch) {
		this.btnSearch = btnSearch;
	}
	public JList getjListPostOffice() {
		return jListPostOffice;
	}
	public void setjListPostOffice(JList jListPostOffice) {
		this.jListPostOffice = jListPostOffice;
	}
	public JButton getBtnDelete() {
		return btnDelete;
	}
	public void setBtnDelete(JButton btnDelete) {
		this.btnDelete = btnDelete;
	}
	public ListModel getJListModel() {
		return JListModel;
	}
	public void setJListModel(ListModel jListModel) {
		JListModel = jListModel;
	}
	
	private class ActionSearchPostOffices implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performSearchPostOffices();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionDeletePostOffice implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performDeletePostOffice();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionUpdatePostOffice implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performUpdatePostOffice();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionSelectItem implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			try {
				controller.performSelectItem();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}

	
	
}